package cn.gson.crmdemo.controller.privilege;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.gson.crmdemo.model.pojo.Emp;
import cn.gson.crmdemo.model.pojo.Notice;
import cn.gson.crmdemo.model.service.PrivilegeService;

@Controller
public class NoticeController {
	@Autowired
	PrivilegeService ps; 
	/**
	 * 发布通知
	 * @param notice
	 */
	@RequestMapping("add-notice")
	public String addNotice(Notice notice,HttpServletRequest request) {
		Emp emp = (Emp) request.getSession().getAttribute("emp");
		notice.setEmp(emp);
		System.err.println(emp.getEmpId());
		ps.addNotice(notice);
		return "asdfg";
	}
	/**
	 * 查询所有通知
	 * @return
	 */
	@RequestMapping("notice-All")
	@ResponseBody
	public Map<String, Object> selectNoticeAll(Integer page,Integer pageSize,
			String objects,Integer selectType) {
		System.err.println(objects);
		System.err.println(selectType);
		Page<Object> pages = PageHelper.startPage(page,pageSize);
		JSONArray list = ps.selectNoticeAll(objects,selectType);
		long total = pages.getTotal();
		Map<String, Object> map = new HashMap<>();
		map.put("list", list);
		map.put("total", total);
		return map;
	}
	/**
	 * 删除通知
	 * @param noticeId
	 * @return
	 */
	@RequestMapping("delete-notice")
	public String deleteNotice(Integer noticeId) {
		ps.deleteNotice(noticeId);
		return "redirect:notice-All?page=1&pageSize=7&objects=&selectType=1";
	}
	/**
	 * 根据编号查询通知
	 * @return
	 */
	@RequestMapping("select-notices")
	@ResponseBody
	public JSONArray selectNotices(Integer noticeId) {
		return ps.selectNotices(noticeId);
	}
}
